package com.js.nickcar.module.addcar.brand;

import android.text.TextUtils;

import com.js.nickcar.api.HttpHelpter;
import com.js.nickcar.api.UrlConfig;
import com.js.nickcar.api.UrlUtils;
import com.js.nickcar.base.ApiCallback;
import com.js.nickcar.base.BasePresenter;
import com.js.nickcar.base.HttpResultFunc;
import com.js.nickcar.entity.remote.ApiResultBean;
import com.js.nickcar.entity.remote.CarBrandBean;
import com.zyzd.commontools.log.TLog;

import java.util.ArrayList;
import java.util.Map;
import java.util.TreeMap;

import rx.Observable;


/**
 * Created by LZY on 2017/9/29.
 */
public class CarBrandPresenter extends BasePresenter<CarBrandView> implements CarBrandBean.TYPE {

    public void requestData() {
        Observable<ApiResultBean<TreeMap<String, ArrayList<CarBrandBean>>>> carBrand = HttpHelpter.getApiService().getCarBrand(UrlUtils.getBaseRequestMap(UrlConfig.API_APPRAISER_CAR_BRAND));
        addSubscription(carBrand.map(new HttpResultFunc<TreeMap<String, ArrayList<CarBrandBean>>>()), getCarBrandScriber());
    }

    public ApiCallback<TreeMap<String, ArrayList<CarBrandBean>>> getCarBrandScriber() {
        return new ApiCallback<TreeMap<String, ArrayList<CarBrandBean>>>() {

            @Override
            public void onSuccess(TreeMap<String, ArrayList<CarBrandBean>> map) {
                TLog.d("zyzd", "CarBrandPresenter>>onSuccess--> " + map);
                if (map.containsKey("hot") && map.get("hot") != null) {
                    mView.updateHot(map.get("hot"));
                    map.remove("hot");
                }
                ArrayList<CarBrandBean> carBrandLists = new ArrayList<>();
                for (Map.Entry<String, ArrayList<CarBrandBean>> entry : map.entrySet()) {

                    String key = entry.getKey();
                    ArrayList<CarBrandBean> value = entry.getValue();

                    if (TextUtils.isEmpty(key) || value == null || value.size() == 0) {
                        continue;
                    }
                    CarBrandBean letterBean = new CarBrandBean();
                    letterBean.setType(TYPE_LETTER);
                    letterBean.setPin(key);
                    carBrandLists.add(letterBean);
                    carBrandLists.addAll(value);
                }
                mView.updateCarBrand(carBrandLists);
            }

            @Override
            public void onFailure(String msg) {
                mView.showToast(msg);
                TLog.d("zyzd", "CarBrandPresenter>>onFailure--> " + msg);
            }

            @Override
            public void onFinish() {
                mView.onComplete();
                TLog.d("zyzd", "CarBrandPresenter>>onFinish--> ");
            }
        };
    }

}
